package com.ziyun.erp.web.modules.member.controller;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.member.entity.ConfigMemberLevelEntity;
import com.ziyun.erp.modules.member.service.ConfigMemberLevelService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 用户等级配置表
 *
 * @author jiangli
 * @email
 * @date 2018-08-01 17:15:41
 */
@RestController
@RequestMapping("member/configmemberlevel")
public class ConfigMemberLevelController {
    @Autowired
    private ConfigMemberLevelService configMemberLevelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:configmemberlevel:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = configMemberLevelService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{memberLvId}")
    @RequiresPermissions("member:configmemberlevel:info")
    public R info(@PathVariable("memberLvId") Integer memberLvId) {
        ConfigMemberLevelEntity configMemberLevel = configMemberLevelService.selectById(memberLvId);

        return R.ok().put("configMemberLevel", configMemberLevel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:configmemberlevel:save")
    public R save(@RequestBody ConfigMemberLevelEntity configMemberLevel) {
        configMemberLevelService.insert(configMemberLevel);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:configmemberlevel:update")
    public R update(@RequestBody ConfigMemberLevelEntity configMemberLevel) {
        ValidatorUtils.validateEntity(configMemberLevel);
        configMemberLevelService.updateAllColumnById(configMemberLevel);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:configmemberlevel:delete")
    public R delete(@RequestBody Integer[] memberLvIds) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        configMemberLevelService.deleteByIds(memberLvIds,curentUser);
        return R.ok();
    }


    @PostMapping("/setDefault/{memberLvId}")
    @RequiresPermissions("member:configmemberlevel:update")
    public Map<String, Object> setDefault(@PathVariable("memberLvId") Integer memberLvId) {
        Map<String, Object> map = configMemberLevelService.setDefault(memberLvId);
        return map;
    }
}
